﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;

namespace DAL
{
    public class AutoSettingDAL:Base<AutoSetting>
    {
        public AutoSettingDAL() : base("auto_set","ID") { }

        public override bool Insert(AutoSetting entity)
        {
            string sql = "insert into `auto_set` (`AID`,`AutoType`,`Money`,`Card_ID`,`Category_ID`,`Remark`,`Last_Update_Time`) values(@aid,@atype,@m,@cardid,@cid,@r,now())";
            var r = MySQLHelper.ExecuteNonQuery(sql, GetParams("aid", entity.AID),
                GetParams("atype", entity.AutoType),
                GetParams("m", entity.Model.Money),
                GetParams("cardid", entity.Model.CardID),
                GetParams("cid", entity.Model.CategoryID),
                GetParams("r", entity.Model.Remark));
            return r == 1;
        }

        public override bool Update(AutoSetting entity)
        {
            string sql = @"update `auto_set` set `AutoType`=@atype, `Money` = @m, `Card_ID` =@cardid, `Category_ID`=@cid,`Remark`=@r,`Last_Update_Time` = @time where id=@id" ;
            var r = MySQLHelper.ExecuteNonQuery(sql, 
                GetParams("atype", entity.AutoType),
                GetParams("m", entity.Model.Money),
                GetParams("cardid", entity.Model.CardID),
                GetParams("cid", entity.Model.CategoryID),
                GetParams("r", entity.Model.Remark),
                GetParams("time", entity.LastTime),
                GetParams("id", entity.ID));
            return r == 1;
        }

        public override AutoSetting GetEntityById(int id)
        {
            throw new NotImplementedException();
        }

        public override AutoSetting GetEntityById(string id)
        {
            throw new NotImplementedException();
        }

        public List<AutoSetting> QueryByAID(int aid)
        {
            string sql = "select * from `auto_set` where `aid`=@aid";
            var table = MySQLHelper.Query(sql, GetParams("aid", aid));
            List<AutoSetting> list = new List<AutoSetting>();
            foreach (DataRow row in table.Rows)
            {
                AutoSetting item = new AutoSetting()
                {
                    AID = aid,
                    AutoType = int.Parse(row["AutoType"].ToString()),
                    ID = int.Parse(row["ID"].ToString()),
                    LastTime = DateTime.Parse(row["Last_Update_Time"].ToString()),
                    Model = new Record()
                    {
                        AccountID = aid,
                        CardID = int.Parse(row["Card_ID"].ToString()),
                        CategoryID = int.Parse(row["Category_ID"].ToString()),
                        Remark = row["remark"].ToString(),
                        Money = decimal.Parse(row["Money"].ToString())
                    }
                };
                list.Add(item);
            }
            return list;
        }
    }
}
